package com.et.ball.view
{
 
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.Socket;
	import flash.ui.Keyboard;
	import flash.utils.ByteArray;
	import com.et.ball.module.Rect;
	import com.et.ball.module.Circle;
	import com.et.ball.module.Ball;
	
	public class GameLayer extends View  
	{
		private var _game:Game;
		
		//声明两个球拍和一个小球；
		private var downHitter:Ball;
		private var upHitter:Ball;
		private var ball:Ball;
		//声明球袋；
		private var downBallBag:Rect;
		private var upBallBag:Rect;
		//声明运动参数；
		private var bounce:Number=-0.7;
		private var spring1:Number=2;
		private var spring2:Number=0.2;
		private var easing:Number=0.3;
		
	 
		
		public function GameLayer(game:Game)
		{
			_game=game; 
			super()
		}
		
		override public function init():void
		{
			//添加小球和球拍；
			ball=new Ball(12,0);
			ball.x=(_game.stage.stageWidth)/2;
			ball.y=(_game.stage.stageHeight)/2;
			ball.vx=0;
			ball.vy=0;
			ball.mass=2;
			addChild(ball);
			
			downHitter=new Ball(30,0x000000);
			upHitter=new Ball(30,0xff0000);
			downHitter.x=(_game.stage.stageWidth)/2;
			downHitter.y=(_game.stage.stageHeight-downHitter.height);
			upHitter.x=(_game.stage.stageWidth )/2;
			upHitter.y=upHitter.height;
			addChild(downHitter);
			addChild(upHitter);
			upHitter.vx=0;
			upHitter.vy=0;
			upHitter.mass=4;
			
			//画上球袋；
			downBallBag=new Rect();
			upBallBag=new Rect();
			downBallBag.x=(_game.stage.stageWidth-downBallBag.width)/2;
			downBallBag.y=_game.stage.stageHeight-5;
			upBallBag.x=(_game.stage.stageWidth-upBallBag.width)/2;
			upBallBag.y=-15;
			addChildAt(downBallBag,0);
			addChildAt(upBallBag,0);
			
			//画上界限；
			var circle:Circle=new Circle();
			addChildAt(circle,0);
			circle.x=(_game.stage.stageWidth )/2
			circle.y=(_game.stage.stageHeight )/2	
			graphics.lineStyle(1,0x000000,.1);
			graphics.moveTo(0,_game.stage.stageHeight/2);
			graphics.lineTo(_game.stage.stageWidth,_game.stage.stageHeight/2);
			
			PlayGame();
		}
		
		//进入运动程序；
		private function PlayGame():void {
			ball.visible=true;
			downHitter.visible=true;
			upHitter.visible=true;
			_game.stage.addEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);
			_game.stage.addEventListener(Event.ENTER_FRAME,onEnterFrame);
		}
		
		private function StopGame():void{
			_game.stage.removeEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);
			_game.stage.removeEventListener(Event.ENTER_FRAME,onEnterFrame);
			ball.visible=false;
			downHitter.visible=false;
			upHitter.visible=false;
			downHitter.x=(_game.stage.stageWidth)/2;
			downHitter.y=(_game.stage.stageHeight-downHitter.height);
			upHitter.x=(_game.stage.stageWidth )/2;
			upHitter.y=upHitter.height;
		}
		
		private function onMouseMove(event:MouseEvent):void {
			trace(downHitter.x,downHitter.y,this.mouseX,this.mouseY)
			//保证down球拍1不出界；
			if (mouseX<downHitter.Radius) {
				//	downHitter.x=downHitter.Radius;
			} else if (mouseX>_game.stage.stageWidth-downHitter.Radius) {
				//	downHitter.x=_game.stage.stageWidth-downHitter.Radius;
			} else if (mouseY<_game.stage.stageHeight/2+downHitter.Radius) {
				downHitter.x=mouseX;
			} else if (mouseY>_game.stage.stageHeight-downHitter.Radius) {
				downHitter.x=mouseX;
			} else {
				downHitter.x=mouseX;
				downHitter.y=mouseY;
			}
		}
		private function onEnterFrame(event:Event):void {
			
			//down球拍1撞击小球；
			ball.x+=ball.vx;
			ball.y+=ball.vy;
			upHitter.x+=upHitter.vx;
			upHitter.y+=upHitter.vy;
			var dx1:Number=ball.x-downHitter.x;
			var dy1:Number=ball.y-downHitter.y;
			var dist1:Number=Math.sqrt(dx1*dx1+dy1*dy1);
			var angle1:Number=Math.atan2(dy1,dx1);
			var miniDist1:Number=downHitter.Radius+ball.Radius;
			if (dist1<miniDist1) {
				var targetX:Number=Math.cos(angle1)*miniDist1+downHitter.x;
				var targetY:Number=Math.sin(angle1)*miniDist1+downHitter.y;
				var ax1=(targetX-ball.x)*spring1;
				var ay1=(targetY-ball.y)*spring1;
				ball.vx+=ax1;
				ball.vy+=ay1;
			}
			//up球拍2行为；
			if ((ball.y<_game.stage.stageHeight/2) && (upHitter.y<ball.y)) {
				upHitterHit();
			} else {
				upHiterEasing();
			}
			//失败后出现标语；
			if (!ball.hitTestObject(downBallBag)) {
				checkSide();
			} else { //失败
				trace("失败")
				StopGame()
				this.dispatchEvent(new Event("replay"));
			}
			//胜利后出现胜利标语；
			if (!ball.hitTestObject(upBallBag)) {
				checkSide();
			} else { //胜利
				trace("胜利")
				StopGame()
				this.dispatchEvent(new Event("replay"));
			}
		}
		 
		//判断小球与墙壁反弹；
		private function checkSide():void {
			if (ball.x-ball.Radius<0) {
				ball.x=ball.Radius;
				ball.vx*=bounce;
			} else if (ball.x+ball.Radius>_game.stage.stageWidth) {
				ball.x=_game.stage.stageWidth-ball.Radius;
				ball.vx*=bounce;
			}
			if (ball.y-ball.Radius<0) {
				ball.y=ball.Radius;
				ball.vy*=bounce;
			} else if (ball.y+ball.Radius>_game.stage.stageHeight) {
				ball.y=_game.stage.stageHeight-ball.Radius;
				ball.vy*=bounce;
			}
		}
		//up球拍击球行为；
		private function upHitterHit():void {
			var dx2:Number=ball.x-upHitter.x;
			var dy2:Number=ball.y-upHitter.y;
			var dist2:Number=Math.sqrt(dx2*dx2+dy2*dy2);
			var miniDist2:Number=upHitter.Radius+ball.Radius;
			var angle2:Number=Math.atan2(dy2,dx2);
			var force:Number=upHitter.mass*ball.mass/dist2*dist2;
			var a:Number=force/upHitter.mass;
			upHitter.vx=15*a*Math.cos(angle2);
			upHitter.vy=15*a*Math.sin(angle2);
			if (dist2<miniDist2) {
				var targetX2:Number=Math.cos(angle2)*miniDist2+300;
				var targetY2:Number=Math.sin(angle2)*miniDist2+30;
				var ax2=(targetX2-ball.x)*spring2;
				var ay2=(targetY2-ball.y)*spring2;
				upHitter.vx+=ax2;
				upHitter.vy+=ay2;
				ball.vx-=ax2;
				ball.vy-=ay2;
			}
		}
		private function upHiterEasing():void {
			var dx3:Number=300-upHitter.x;
			var dy3:Number=25-upHitter.y;
			upHitter.x+=dx3*easing;
			upHitter.y+=dy3*easing;
		}
		
		override public function setData():void
		{
			
		}
	}
}